package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.modules.dic.service.IDicConstantsService;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorConfig;
import org.jeecg.common.modules.doctor.mapper.DoctorConfigMapper;
import org.jeecg.common.modules.doctor.service.IDoctorConfigService;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

import static org.jeecg.common.modules.dic.util.DicConstant.DOC_TEXT_PRICE;
import static org.jeecg.common.modules.dic.util.DicConstant.DOC_VIDEO_PRICE;

/**
 * @Description: doctor_config
 * @Author: jeecg-boot
 * @Date:   2020-08-15
 * @Version: V1.0
 */
@Service
public class DoctorConfigServiceImpl extends ServiceImpl<DoctorConfigMapper, DoctorConfig> implements IDoctorConfigService {

    @Resource
    private IDicConstantsService dicConstantsService;
    @Resource
    private IDoctorService doctorService;


    @Override
    public DoctorConfig getByDoctorId(String doctorId,Boolean create) {
        LambdaQueryWrapper<DoctorConfig> query = Wrappers.lambdaQuery();
        query.eq(DoctorConfig::getDoctorId,doctorId);
        DoctorConfig exits =  getOne(query);
        if (Objects.isNull(exits) && create){
            exits = new DoctorConfig();
            exits.setDoctorId(doctorId);
            exits.setFreeMessageCount(5);
            exits.setWelcomeText("欢迎关注我在大医智慧的工作室，遇到问题可以在线咨询、预约挂号。");
            exits.setTextMoney(new BigDecimal(dicConstantsService.getByKey(DOC_TEXT_PRICE)));
            exits.setVideoMoney(new BigDecimal(dicConstantsService.getByKey(DOC_VIDEO_PRICE)));
            save(exits);
        }
        return exits;
    }

    @Override
    public Doctor registerRatioCheck(String doctorId){
        LambdaQueryWrapper<DoctorConfig> query = Wrappers.lambdaQuery();
        query.eq(DoctorConfig::getDoctorId,doctorId);
        DoctorConfig doctorConfig = getOne(query);
        if(Objects.nonNull(doctorConfig.getRegisterRatio())){
            return doctorService.getById(doctorId);
        }else{
            return null;
        }
    }
}
